package com.bs.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.bs.bean.Product;
import com.bs.service.ProductService;


@SuppressWarnings("serial")
@ParentPackage("struts-default")
@Namespace("/product")
public class ProductAction extends ActionBase {

	private Product product;
	private ProductService productService;
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public ProductService getProductService() {
		return productService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	@Action("save")
	public void save() throws Exception{
		try {
			this.productService.save(product);
			response.getWriter().write(this.successInfo("添加货品信息成功"));
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("添加货品信息失败，可能货品名称已存在"));
			e.printStackTrace();
		}
	}
	@Action("delete")
	public void batchDelete() throws Exception{
		try {
			String ids = (String) request.getParameter("ids");
			int rows = this.productService.batchDelete(ids);
			if(rows >= 0){
				response.getWriter().write(this.successInfo("删除货品信息成功"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("货品信息正在被使用，不可删除"));
			e.printStackTrace();
		}
	}
	
	@Action("query")
	public void query() throws Exception{
		try {
			List<Product> list = this.productService.query(product);
			response.getWriter().write(JSONArray.fromObject(list).toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	

}
